<template>
	<div class="app-container">
		<el-row>
			<el-col :span="8" style="padding-right: 10px;">
				<!--<device-tree :showCheckbox="true" :checkStrictly="true" :render-content="renderContent"></device-tree>-->
				<device-tree ref="deviceTreeVue" :showCheckbox="false" :checkStrictly="false"
					@handleNodeClick="handleNodeClick"></device-tree>
			</el-col>

			<el-col :span="4">
				<el-button-group>
					<el-button type="primary" icon="el-icon-arrow-left" @click="handleBindDept">到左边</el-button>
					<el-button type="primary" @click="handleUnBindDept">到右边<i
							class="el-icon-arrow-right el-icon--right"></i></el-button>
				</el-button-group>
			</el-col>

			<el-col :span="12" style="padding-left: 10px;">
				<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
					label-width="68px">
					<el-form-item label="设备名称" prop="name">
						<el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item label="设备ip" prop="ip">
						<el-input v-model="queryParams.ip" placeholder="请输入设备ip" clearable
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item>
						<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
						<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</el-form>

				<el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" align="center" />
					<el-table-column label="设备编码" align="center" prop="id" />
					<el-table-column label="设备名称" align="center" prop="name" />
					<el-table-column label="设备ip" align="center" prop="ip" />
				</el-table>

				<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
					:limit.sync="queryParams.pageSize" @pagination="getList" />

			</el-col>
		</el-row>


	</div>
</template>
<style scoped></style>

<script>
import { listDevice, delDevice, bindDept,unBindDept } from "@/api/vplat/device";
import deviceTree from '@/components/vplat/DeviceTree/deviceTree';

export default {
	name: "Bind",
	components: {//使用自定义组件
		deviceTree,
	},
	dicts: ['sys_oper_type', 'sys_normal_disable', 'vplat_device_online', 'vplat_device_factory', 'vplat_device_protocol'],
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 设备表格数据
			deviceList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 是否仅读
			isOnlyRead: true,
			//选择的节点
			selectNode: null,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				unBind: true,
				gatewayId: null,
				parentId: null,
				name: null,
				ip: null,
				port: null,
				username: null,
				password: null,
				factory: null,
				type: null,
				protocol: null,
				longitude: null,
				latitude: null,
				address: null,
				online: null,
				status: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 自定义渲染过程 */
		/*
		renderContent(h, { node, data, store }) {
			if (data.isLeafNode) {
				return (
					<span id={data.id}>{node.label}</span>
				);
			} else {
				return <span>{node.label}</span>;
			}
		},
		*/
		/** 点击节点回调 */
		handleNodeClick(node) {
			this.selectNode = node;
		},
		/** 过滤协议 */
		filter(dict) {
			if (this.title == '添加设备' && dict.value == '2') {
				return false;
			}
			return true;
		},
		/** 查询设备列表 */
		getList() {
			this.loading = true;
			listDevice(this.queryParams).then(response => {
				this.deviceList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				gatewayId: null,
				parentId: null,
				name: null,
				ip: null,
				port: null,
				username: null,
				password: null,
				factory: null,
				type: null,
				protocol: null,
				longitude: null,
				latitude: null,
				address: null,
				online: null,
				status: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				remark: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 挂载按钮操作 */
		handleBindDept() {
			//console.log(this.selectNode, this.ids);
			if (this.selectNode == null) {
				this.$modal.msgError("未选择挂载节点");
				return;
			}
			if (!this.selectNode.isLeafNode) {
				this.$modal.msgError("需要选择最底层的组织节点");
				return;
			}
			if (this.ids == null || this.ids.length == 0) {
				this.$modal.msgError("需要选择挂载的设备");
				return;
			}

			this.loading = true;
			let params = {
				deptId: this.selectNode.id,
				devices: this.ids,
			};
			bindDept(params).then(response => {
				this.$refs.deviceTreeVue.getDeviceList(this.selectNode);
				this.getList();
				this.$modal.msgSuccess("挂载成功");
				this.loading = false;
			});


		},
		/** 解除挂载按钮操作 */
		handleUnBindDept() {
			//console.log(this.selectNode);
			if (this.selectNode == null || this.selectNode.nodeType !== 'IPC') {
				this.$modal.msgError("未选择设备节点");
				return;
			}
			this.loading = true;
			let params = {
				deptId: this.selectNode.deptId,
				devices: [this.selectNode.id],
			};
			unBindDept(params).then(response => {
				this.$refs.deviceTreeVue.getDeviceList(this.selectNode);
				this.getList();
				this.$modal.msgSuccess("解除成功");
				this.loading = false;
			});

		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$modal.confirm('是否确认删除设备编号为"' + ids + '"的数据项？').then(function () {
				return delDevice(ids);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => { });
		},
	}
};
</script>
